import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.math.BigInteger;

import javax.swing.text.AbstractDocument;
import javax.swing.text.MaskFormatter;

public class Employee extends Person implements ActionListener{

	JButton okE;
	JButton closeE;
        JFrame e;
        JFrame s;
        JFrame supe;
        JFrame j;
        long eIDI;
        double salary;
	private String eID;
	private String supervisor;
	private String job;
	JTextField eIDT;
	JTextField supeT;
	JTextField jobT;
	JTextField salaryT;
	public Employee() throws Exception
	{	
                eIDI=0;
		eID="";
		supervisor="";
		job="";
		salary=0.00;
		setLayout(new GridLayout(10, 4, 5, 5));
		JLabel eIDL= new JLabel("Employee ID: ");
                eIDT= new JTextField(12);
                ((AbstractDocument) eIDT.getDocument()).setDocumentFilter(new EIDFieldDocumentFilter());
		JLabel supeL= new JLabel("Supervisor: ");
		supeT= new JTextField(25);
                ((AbstractDocument) supeT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
		JLabel jobL= new JLabel("Job: ");
                jobT= new JTextField(25);
                ((AbstractDocument) jobT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
		JLabel salaryL= new JLabel("Salary: ");
                salaryT= new JTextField(12);
                ((AbstractDocument) salaryT.getDocument()).setDocumentFilter(new SalaryFieldDocumentFilter());
		okE=new JButton("OK");
		closeE=new JButton("Close");
		okE.setActionCommand("OK");
		closeE.setActionCommand("CLOSE");
		okE.addActionListener(this);
		closeE.addActionListener(this);
		add(eIDL);
		add(eIDT);
		add(supeL);
		add(supeT);
		add(jobL);
		add(jobT);
		add(salaryL);
		add(salaryT);
		add(okE);
		add(closeE);
	}
        public boolean validateSupervisor() {
            boolean valid=true;
            boolean size=true;
            char[] temp=supeT.getText().trim().toCharArray();
            for(int i=0; i<temp.length;i++) {
                if(temp[i]<'A' || temp[i]>'Z')
                {
                    if(temp[i]<'a' || temp[i]>'z')
                    {
                        if(temp[i]!=' ')valid=false;
                    }
                }
            }
            if(temp.length>25 || temp.length==0)size=false;
            if(valid==false || size==false) {
                supe=new JFrame("Supervisor Error Message Box");
                supe.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("SUPERVISORERRORCLOSE");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("Only letters and spaces!");   
                }else if(valid==false && size==false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                }else if(valid==true && size==false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                supe.add(eL);
                supe.add(close);
                supe.setSize(400,300);
                supe.setLocationRelativeTo(null);
                supe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                supe.setVisible(true);
            }
            if(valid&&size) {
                setSupervisor(supeT.getText().trim());
            }
            return (valid && size);   
        }
        public boolean validateJob() {
            boolean valid=true;
            boolean size=true;
            char[] temp=jobT.getText().trim().toCharArray();
            for(int i=0; i<temp.length;i++) {
                if(temp[i]<'A' || temp[i]>'Z')
                {
                    if(temp[i]<'a' || temp[i]>'z')
                    {
                        if(temp[i]!=' ')valid=false;
                    }
                }
            }
            if(temp.length>25 || temp.length==0)size=false;
            if(valid==false || size==false) {
                j=new JFrame("Job Error Message Box");
                j.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("JOBERRORCLOSE");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("Only letters and spaces!");   
                }else if(valid==false && size==false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                }else if(valid==true && size==false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                j.add(eL);
                j.add(close);
                j.setSize(400,300);
                j.setLocationRelativeTo(null);
                j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                j.setVisible(true);
            }
            if(valid&&size) {
                setJob(jobT.getText().trim());
            }
            return (valid && size);   
        }
        public boolean proceedOnward() 
        {
            boolean valid=true;
            if(validateEID() ==false) valid=false;
            if(validateSalary()==false) valid=false;
            if(validateSupervisor()==false)valid=false;
            if(validateJob()==false) valid=false;
            if(validateFName()==false) valid=false;
            if(validateMName()==false)valid=false;
            if(validateLName()==false) valid=false;
            if(validateAge()==false) valid=false;
            if(validateSSN()==false) valid=false;
            return valid;
        }
        public boolean validateEID() {
            boolean valid=true;
            boolean size=true;
            char[] temp=eIDT.getText().toCharArray();
            for(int i=0; i<temp.length;i++) {
                if(temp[i]<'0' || temp[i]>'9')valid=false;
            }
            if(temp.length!=12)size=false;
            if(valid==false || size==false) {
                e=new JFrame("Employee ID Error Message");
                e.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("EIDERRORCLOSE");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("Only numbers!");   
                }else if(valid==false && size==false) {
                    eL.setText("Only numbers! Twelve numbers to be exact.");
                }else if(valid==true && size==false) {
                    eL.setText("Only twelve numbers, no more no less than twelve.");
                }
                e.add(eL);
                e.add(close);
                e.setSize(400,300);
                e.setLocationRelativeTo(null);
                e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                e.setVisible(true);
            }
            if(valid&&size) {
                setEID(Long.valueOf(eIDT.getText()).longValue());
            }
            return (valid && size);
        }
	public void initialize()
	{
		this.setTitle("Employee");
		remove(ok);
		remove(close);
		this.setSize(400,300);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
        public boolean validateSalary() {
            boolean valid=true;
            boolean size=true;
            char[] temp=salaryT.getText().toCharArray();
            for(int i=0; i<=temp.length-4;i++) {
                if(temp[i]<'0' || temp[i]>'9')valid=false;
            }
            if(temp.length>=4) {
                    
                if(temp[temp.length-(2+1)]!='.') {
                    valid=false;
                }else if(temp[temp.length-(2+1)]=='.') {
                    for(int i=temp.length-2; i<temp.length; i++) {
                        if(temp[i]<'0' || temp[i]>'9')valid=false;
                    }
                }
            }else valid=false;    
            if(temp.length>12)size=false;
            if(valid==false || size==false) {
                s=new JFrame("Salary Error Message Box");
                s.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("SERRORCLOSE");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("The format is number.cents (Example: 1000000.00).");   
                }else if(valid==false && size==false) {
                    eL.setText("The format is number.cents (Example: 1000000.00). \nLess than twelve characters, more than none.");
                }else if(valid==true && size==false) {
                    eL.setText("Less than twelve characters, more than none at all.");
                }
                s.add(eL);
                s.add(close);
                s.setSize(700,300);
                s.setLocationRelativeTo(null);
                s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                s.setVisible(true);
            }
            if(valid&&size) {
                setSalary(Double.parseDouble(salaryT.getText()));
            }
            return (valid && size);
        }
	public void actionPerformed(ActionEvent event)
	{
		String command=event.getActionCommand();
		if(command=="OK")
		{
                        if(proceedOnward())
                        {
                            System.out.println(toString());
                            System.exit(0);
                            dispose();
                        }
		}
		else if(command=="CLOSE")
		{
			dispose();
                        Choose c1=new Choose();
                        c1.initialize();
		}
                else if(command=="EIDERRORCLOSE") 
                {
                        e.dispose();
                }
                else if(command=="SERRORCLOSE") 
                {
                        s.dispose();           
                }
                else if(command=="SUPERVISORERRORCLOSE") 
                {
                        supe.dispose();
                }
                else if(command=="JOBERRORCLOSE") 
                {
                        j.dispose();
                }
	}
	public long getEID()
	{
		return eIDI;
	}
	public String getSupervisor()
	{
		return supervisor;
	}
	public String getJob()
	{
		return job;
	}
	public double getSalary()
	{
		return salary;
	}
        public void setEID(long temp) 
        {
                eIDI=temp;
        }
        public void setSupervisor(String temp) 
        {
                supervisor=temp;
        }
        public void setJob(String temp) 
        {
                job=temp;
        }
        public void setSalary(double temp) 
        {
                salary=temp;
        }
        public String stringSalary() 
        {
                String newSalary="";
                String temp=String.valueOf(getSalary());
                char[] tempSalary=temp.toCharArray();
                char[] tSalary=tempSalary;
                int decimal=0;
                int e=0;
                for(int i=0;i<tempSalary.length;i++) 
                {
                    if(tempSalary[i]=='.') 
                    {
                        decimal=i;
                    }
                    if(tempSalary[i]=='E') 
                    {
                        e=i;
                    }
    
                }
                if(e!=0) 
                {
                    int tempE=Integer.parseInt(String.valueOf(tempSalary[e+1]));
                    if((decimal+tempE)==(tempSalary.length-(4)))
                    {
                        newSalary="0";
                    }
                    for(int i=tempSalary.length-(2+1);i>=0;i--) 
                    {
                        if(i==(decimal+tempE)) 
                        {
                            newSalary="." +  newSalary;
                        }
                        if(tempSalary[i]!='.') 
                        {
                            if(i<(decimal+tempE) && ((decimal+tempE+1)-i)%(2+1)==0) {
                                newSalary= String.valueOf(tempSalary[i])+newSalary;
                                if(i!=0)newSalary=","+newSalary;
                            }
                            else newSalary=String.valueOf(tempSalary[i]) +  newSalary;
                        }
                    }
                }
                else
                {
                    if(decimal>tempSalary.length-(2+1)) {
                    newSalary+="0";
                    }
                    for(int i=tempSalary.length-1;i>=0;i--) {
                        if(i<decimal && (i-decimal)%(2+1)==0) {
                            newSalary= String.valueOf(tempSalary[i])+newSalary;
                            if(i!=0)newSalary=","+newSalary;
                        }
                        else newSalary= String.valueOf(tempSalary[i])+newSalary;
                    }
                }
                newSalary="$"+newSalary;
                return newSalary;
        }
	public String toString()
	{
		return "\nFirst Name: " + getFName() + "\nLast Name: " + getLName() + "\nMiddle Name: " + getMName() + "\nAge:" + getAge() + "\nSSN: " + stringSSN()+"\nEmployee ID: "+ getEID()+ "\nSupervisor: " + getSupervisor()+ "\nJob: "+getJob()+ "\nSalary: "+ stringSalary();
	}
}